package com.tgpms.web.assets.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.DeleteException;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.exception.QueryException;
import com.tgpms.exception.UpdateException;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.web.assets.entity.FixedAsset;
import com.tgpms.web.assets.service.FixedAssetService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author newway
 * <p>
 * 固定资产登记接口接口
 */
@Api(value = "FixedAssetController", tags = "固定资产登记接口")
@RestController
@RequestMapping(value = "/ev/fixedAsset")
public class FixedAssetController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private FixedAssetService fixedAssetService;
	@Autowired
	private TokenService tokenService;
	
	
	/**
	* @Author newway
	* @Description 查询固定资产登记接口信息
	* @Date 2019年8月2日上午8:21:38
	* @Param pageNo
	* @Param 
	* @return com.tgpms.common.Result
	**/
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "查询固定资产登记接口信息", notes = "查询固定资产登记接口信息", httpMethod = "POST")
	@PostMapping(value = "/selFixedAssetList")
	public Result selFixedAssetList(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
			@RequestParam(value = "condition", required = false) String condition,
			@RequestParam(value = "dataowner", required = false) String dataowner,
			@RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize) {
		Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = fixedAssetService.selFixedAssetList(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}
	
	/**
	* @Author newway
	* @Description 添加固定资产登记接口信息
	* @Date 2019年8月1日下午4:22:08
	* @Param com.tgpms.web.assets.entity.FixedAsset
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "添加固定资产登记接口信息", notes = "添加固定资产登记接口信息", httpMethod = "POST")
	@PostMapping(value = "/addFixedAssetInfo")
	public Result addFixedAssetInfo(@RequestBody FixedAsset fixedAsset,HttpServletRequest request) {
		Result result = new Result();
		try {
			//获取token操作人
			fixedAsset.setInsertUser(tokenService.getUserName(request));
			fixedAsset.setInsertDate(new Date());
			Map<String, String> map = fixedAssetService.addFixedAssetInfo(fixedAsset);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
				result.setData(map.get("data"));
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 更新固定资产登记接口信息
	* @Date 2019年8月1日下午5:37:08
	* @Param com.tgpms.web.assets.entity.FixedAsset
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "更新固定资产登记接口信息", notes = "更新固定资产登记接口信息", httpMethod = "PUT")
	@PutMapping(value = "/updateFixedAssetInfo")
	public Result updateFixedAssetInfo(@RequestBody FixedAsset fixedAsset,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(fixedAsset.getFixedAssetNo())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			fixedAsset.setModifyUser(tokenService.getUserName(request));
			fixedAsset.setModifyDate(new Date());
			Map<String, String> map = fixedAssetService.updateFixedAssetInfo(fixedAsset);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 删除固定资产登记接口信息
	* @Date 2019年8月1日下午5:41:57
	* @Param com.tgpms.web.assets.entity.FixedAsset
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "删除固定资产登记接口信息", notes = "删除固定资产登记接口信息", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteFixedAssetInfo")
	public Result deleteFixedAssetInfo(@RequestParam(value = "fixedAssetNo", required = true) String fixedAssetNo) {
		Result result = new Result();
		if(Common.isEmpty(fixedAssetNo)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			Map<String, String> map = fixedAssetService.deleteFixedAssetInfo(fixedAssetNo);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

	
}
